<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      ref="form"
      label-width="140px"
      :model="formData"
      :rules="formRules"
      inline
    >
      <el-form-item label="归属学科" prop="subject_id">
        <el-select
          v-model="formData.subject_id"
          size="small"
          placeholder="归属学科"
          clearable
          @change="changeSubject"
        >
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="归属目录" prop="directory_id">
        <el-select
          v-model="formData.directory_id"
          size="small"
          placeholder="归属目录"
          clearable
          @clear="clearList"
        >
          <el-option
            v-for="item in direList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标签名称" prop="tag_name">
        <el-input v-model="formData.tag_name" placeholder="请输入标签名称" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="formData.sort" placeholder="" />
      </el-form-item>
      <el-form-item label="是否显示" prop="is_show">
        <el-switch
          v-model="formData.is_show"
          active-text="是"
          inactive-text="否"
          :active-value="1"
          :inactive-value="2"
        />
      </el-form-item>
      <el-row type="flex" justify="end">
        <el-button size="small" @click="dialogShow = false">取消</el-button>
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >确定</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addTag, editTag, getTreeList } from '@/api/tag'
export default {
  name: 'AddTag',

  data() {
    return {
      direList: [], // 目录
      list: [], // 归属学科
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        directory_id: '',
        is_show: 1,
        sort: 10, // 排序
        subject_id: '',
        tag_name: ''
      },
      // 表单验证规则
      formRules: {
        subject_id: [
          { required: true, message: '请选择学科', trigger: 'blur' }
        ],
        directory_id: [
          { required: true, message: '请选择目录', trigger: 'blur' }
        ],
        tag_name: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ],
        sort: [{ required: true, trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改标签' : '添加标签'
    }
  },
  created() {
    this.getTreeList()
  },
  methods: {
    // 渲染科目
    async getTreeList() {
      const res = await getTreeList()
      console.log(res)
      this.list = res
    },
    changeSubject(event) {
      // console.log(event)
      const res = this.list.find((item) => {
        return item.id === event
      })
      this.direList = res.children
    },
    // 清空
    // 清空
    clearList() {
      // this.initData()
      this.direList = []
      // this.initData()
    },
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await editTag(this.formData)
        : await addTag(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        directory_id: '',
        is_show: 1,
        sort: 10, // 排序
        subject_id: '',
        tag_name: ''
      }
      // 移除校验
      this.$refs.form.clearValidate()
    }
  }
}
</script>
<style scoped></style>
